package com.kb.design.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kb.design.constant.Constants;
import com.kb.design.entity.DesignDept;
import com.kb.design.entity.DesignRole;
import com.kb.design.entity.DesignTalk;
import com.kb.design.mapper.postgreSQL.DesignRoleMapper;
import com.kb.design.service.DesignRoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色信息表(DesignRole)表服务实现类
 *
 * @author makejava
 * @since 2024-01-17 15:31:33
 */
@Service
@DS("slave-1")
public class DesignRoleServiceImpl extends ServiceImpl<DesignRoleMapper, DesignRole> implements DesignRoleService {
    @Resource
    private DesignRoleMapper designRoleMapper;

    @Override
    public Page<DesignRole> pageList(Page<DesignRole> page, DesignRole designTalk) {
        LambdaQueryWrapper<DesignRole> queryWrapper = Wrappers.lambdaQuery(DesignRole.class);
        queryWrapper.eq(StringUtils.isNotBlank(designTalk.getStatus()),DesignRole::getStatus, designTalk.getStatus());

        if (designTalk.getRoleName() != null) {
            queryWrapper.eq(DesignRole::getRoleName, designTalk.getRoleName());
        }
        queryWrapper.eq(DesignRole::getDelFlag, Constants.DEL_FLAG_NO);
        queryWrapper.orderByAsc(DesignRole::getRoleSort);
        return designRoleMapper.selectPage(page, queryWrapper);
    }

    @Override
    public boolean save(DesignRole designRole) {
        if (designRole.getRoleId() == null) {
            return designRoleMapper.insert(designRole) > 0;
        } else {
            return designRoleMapper.updateById(designRole) > 0;
        }
    }

    @Override
    public DesignRole queryRoleByCode(String code) {
        List<DesignRole> designRoles = designRoleMapper.queryRoleByCode(code);
        List<DesignDept> designDepts = designRoleMapper.queryDeptByCode(code);

        if (!designRoles.isEmpty()) {
         //把roleKey字段,号拼接
            String roleKeys = designRoles.stream()
                    .map(DesignRole::getRoleKey)
                    .collect(Collectors.joining(","));
            //把roleKey字段,号拼接
            String roleNames = designRoles.stream()
                    .map(DesignRole::getRoleName)
                    .collect(Collectors.joining(","));
            DesignRole designRole = designRoles.get(0);
            designRole.setRoleKey(roleKeys);
            designRole.setRoleName(roleNames);
            if (CollectionUtils.isNotEmpty(designDepts)){
                designRole.setRemark(designDepts.get(0).getDeptName());
                designRole.setDepartmentsId(designDepts.get(0).getDeptId());
            }
            return designRole;
        }
        return new DesignRole();
    }
    @Override
    public DesignRole queryRoleByUserCode(String code) {
        List<DesignRole> designRoles = designRoleMapper.queryRoleByCode(code);

        if (!designRoles.isEmpty()) {
            //把roleKey字段,号拼接
            String roleKeys = designRoles.stream()
                    .map(DesignRole::getRoleKey)
                    .collect(Collectors.joining(","));
            //把roleKey字段,号拼接
            String roleNames = designRoles.stream()
                    .map(DesignRole::getRoleName)
                    .collect(Collectors.joining(","));
            DesignRole designRole = designRoles.get(0);
            designRole.setRoleKey(roleKeys);
            designRole.setRoleName(roleNames);
            return designRole;
        }
        return new DesignRole();
    }

    @Override
    public long deleteDeptById(Long id) {
        DesignRole designRole = new DesignRole();
        designRole.setDelFlag("1");
        designRole.setRoleId(id);
        return designRoleMapper.updateById(designRole);

    }
}
